var index = 0;
/**选择父分类时触发*/
function changeParent(parent){
	$('#prizechild'+parent+' > option:gt(0)').remove();
	var id = $("#prizeParent"+parent).val();
	var html = '';
	/**得到该父分类下的子分类*/
	for (var i = 0; i < volist.length; i++) {
		if(volist[i].parent.id==id){
			for (var j = 0; j < volist[i].childList.length; j++) {
				html +='<option  value="'+volist[i].childList[j].id+'">'+volist[i].childList[j].name+'</option>';
			}
		}
	 }
	$('#prizechild'+parent).append(html);
	$("#prizechild"+parent).selectpicker('refresh', true);
	/**得到改父分类下的商品*/
	$('#prize'+parent+' > option:gt(0)').remove();
	var goodhtml='';
	if(id==-1){//如果没有选择父分类。则加载所有的商品
		for (var i = 0; i < goodvolist.length; i++) {
				goodhtml +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
		 }
	}else{
		for (var i = 0; i < goodvolist.length; i++) {
			if(goodvolist[i].parentId==id){
				goodhtml +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
			}
		}
	}
	$('#prize'+parent).append(goodhtml);
	$("#prize"+parent).selectpicker('refresh', true);
}
/**选择子分类时触发*/
function changeChild(child){
	var id = $("#prizechild"+child).val();
	var pid = $("#prizeParent"+child).val();
	/**得到商品*/
	$('#prize'+child+' > option:gt(0)').remove();
	var goodhtml='';
	if(id==-1){//如果没有选择子分类，选择父分类下的所有商品
		if(pid==-1){//也没有选择父分类
			for (var i = 0; i < goodvolist.length; i++) {
					goodhtml +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
			}
		}else{
			for (var i = 0; i < goodvolist.length; i++) {
				if(goodvolist[i].parentId==pid){
					goodhtml +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
				}
			}
		}
	}else{
		for (var i = 0; i < goodvolist.length; i++) {
			if(goodvolist[i].childId==id && goodvolist[i].parentId==pid){
				goodhtml +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
			}
		}
	}
	$('#prize'+child).append(goodhtml);
	$("#prize"+child).selectpicker('refresh', true);
}
/**选择商品时触发*/
function changeprize(prize){
	var id = $("#prize"+prize).val();
	for (var i = 0; i < goodvolist.length; i++) {
		if(goodvolist[i].id==id){
			$("#prizename"+prize).val(goodvolist[i].name);
			$("#prizelink"+prize).val(floorUrl+goodvolist[i].id);
			$("#prizeimg"+prize).attr("src",goodvolist[i].picurl);
			$("#prizeimgurl"+prize).val(goodvolist[i].picurl);
			$("#goodsid"+prize).val(id);
		}
	}
}
$("#addButton").on("click",function(){
	var id = $("#prize"+index).val();
	var flag = $("#flag").val();
	if(id==-1&&flag==1){
		alert("请选择商品");
	}else{
		$("#flag").val(1);
		index++;
		html = '';
		html += '<div id="floorItemList'+index+'" class="col-md-12 floorItemList">';
		html += '<div class="head">';
		html += '<span class="number">'+(index+1)+'</span>';
		html += '<span onclick="removeItem('+index+')" class="remove-btn"><i  class="glyphicon glyphicon-remove"></i>移除</span>';
		html += '</div>';
		html += ' <select disabled="disabled" id="floorItemType0" onchange="addType(0)" style="margin-top: 10px;" class="form-control col-md-12 clearfix">';
		html += '<option value="-1">请选择详情类型</option>';
		html += '<option selected="selected" value="0">商品</option>';
		html += '<option value="1">其他</option>';
		html += '</select> ';
		html += '<div id="itemComment'+index+'"  class="col-md-12 clearfix itemComment">';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">父分类:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<select id="prizeParent'+index+'" onchange="changeParent('+index+')" class="form-control"';
		html += 'data-live-search="true" name="courseTutor.id">';
		html += '<option value="-1">请选择商品父分类</option> ';
		for (var i = 0; i < volist.length; i++) {
				html +='<option  value="'+volist[i].parent.id+'">'+volist[i].parent.name+'</option>';
		 }
		html += '</select> ';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">子分类:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<select id="prizechild'+index+'" onchange="changeChild('+index+')" class="form-control"';
		html += 'data-live-search="true" name="">';
		html += '<option value="-1">请选择商品子分类</option> ';
		html += '</select> ';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">商品:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<select id="prize'+index+'" onchange="changeprize('+index+')" class="form-control"';
		html += 'data-live-search="true" name="">';
		html += '<option value="-1">请选择商品</option> ';
		for (var i = 0; i < goodvolist.length; i++) {
			html +='<option  value="'+goodvolist[i].id+'">'+goodvolist[i].name+'</option>';
		}
		html += '</select> <input id="goodsid'+index+'" type="hidden" value="" name ="pcfloorList['+index+'].goods.id">';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">标题:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<input class="form-control" value="" name="pcfloorList['+index+'].name" id ="prizename'+index+'">';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">链接:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<input class="form-control" value="" name="pcfloorList['+index+'].linkurl" id ="prizelink'+index+'">';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div style="margin-top: 5px;" class="form-group">';
		html += '<label class="control-label col-md-2" for="name">logo:</label>';
		html += '<div class="col-md-10">';
		html += '<div class="clearfix" style="position: relative">';
		html += '<img id="prizeimg'+index+'">';
		html += '<input type="hidden" value="" name="pcfloorList['+index+'].picurl" id ="prizeimgurl'+index+'">';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		$("#floorItem").append(html);
 		$("#prizeParent"+index).selectpicker({
 			dropuAuto : false,
      	    size:4,
  	    	noneResultsText:'没有找到要查找的{0}',
  	        liveSearch: true
 		}); 
 		$("#prizechild"+index).selectpicker({
 			dropuAuto : false,
      	    size:4,
  	    	noneResultsText:'没有找到要查找的{0}',
  	        liveSearch: true
 		}); 
  		$("#prize"+index).selectpicker({
  			dropuAuto : false,
      	    size:4,
  	    	noneResultsText:'没有找到要查找的{0}',
  	        liveSearch: true
  		}); 
  		$("#item").scrollTop(500000);
	}
});

function removeItem(item){
	$("#floorItemList"+item).remove();
}
/*
var index = 0;
*//**因为现在固定只有商品，此方法  暂时不用*//*
function addType(type){
	$("#itemComment"+type+" > div").remove();;
	var itmeType = $("#floorItemType"+type).val();
	alert(itmeType);
	if(itmeType==0){
		$("#itemComment"+type).show();
		//详情为商品
		addPrize();
	}else if (itmeType==1){
		//详情为其他
		$("#itemComment"+type).show();
		addothers();
	}else{
		$("#itemComment"+type).hide();
	}
}
*//**如果选择的商品*//*
function  addPrize(){
	
}
*//**如果选择是其他*//*
function addothers(){
	var html = '<div style="margin-top: 5px;" class="form-group">';
		html+='<label class="control-label col-md-2">标题:</label>';
		html+='<div class="col-md-10">';
		html+='<div class="clearfix">';
		html+='<input type="text" class="form-control"  name="pcfloorList['+index+'].name"  id="itemname'+index+'" value=""/>';
		html+='</div></div></div>';
		html+= '<div class="form-group">';
		html+='<label class="control-label col-md-2">排序:</label>';
		html+='<div class="col-md-10">';
		html+='<div class="clearfix">';
		html+='<input type="text" class="form-control"  name="pcfloorList['+index+'].sortno"  id="itemsortno'+index+'" value=""/>';
		html+='</div></div></div>';
		html+= '<div class="form-group">';
		html+='<label class="control-label col-md-2">链接:</label>';
		html+='<div class="col-md-10">';
		html+='<div class="clearfix">';
		html+='<input type="text" class="form-control"  name="pcfloorList['+index+'].linkurl"  id="itemlinkurl'+index+'" value=""/>';
		html+='</div></div></div>';
		$("#itemComment"+index).append(html);
		
}
$("#addButton").on("click",function(){
	var falg = vilidate();
	if(!falg){
		return ;
	}
	index ++;
	var html = '<div id="floorItemList'+index+'" class="col-md-12 floorItemList">';
	    html += '<select id="floorItemType'+index+'" onchange="addType('+index+')" style="margin-top: 10px;" class="form-control col-md-4">';
	    html += '<option value="-1">请选择详情类型</option>';
	    html += '<option value="0">商品</option>';
	    html += '<option value="1">其他</option>';
	    html += '</select><div id="itemComment'+index+'" style="display: none;" class="col-md-12 clearfix itemComment"></div></div>';
	 *//**验证选项是否填写或选择*//*
	$("#floorItem").append(html);
	$("#item").scrollTop(50000);
});
*//**验证是否讲信息填写完成*//*
function vilidate(){
	return true;
}
*//**请求后台加载商品*//*
function loadPrizr(){
}
*//**加载商品分类*//*
function loadCategory(){
}*/